package com.nd.android.u.ui.chatUiUtils;

import ims.IMSdkEntry;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.text.TextUtils;

import com.common.allCommon.CommonUtils;
import com.common.android.utils.DisplayUtil;
import com.nd.android.u.ui.R;

/**
 * ClassName:LocalImageManager
 * Function: 管理聊天界面里使用的本地图片的缩略图.在聊天界面finish的时候清空.用imageloader管理本地图片的效率不高
 * Reason:	 TODO ADD REASON
 * 
 * @author   cb		
 * @version  v1.3.x
 * @Date	 2014	2014-1-24		上午11:33:06
 *
 * @see 	 
 */
public enum LocalImageManager {
	INSTANCE;
	private static final int MAX_WIDTH=120;
	private Map<Context,ConcurrentHashMap<String, Bitmap>> mBitmapMaps=new HashMap<Context,ConcurrentHashMap<String,Bitmap>>();
	private Bitmap mGifFlagBitmap;
	private Bitmap mStartBitmap;
	/**
	 * @n<b>函数名称</b>     :getGifFlagBitmap
	 * @brief 获取GIF标志
	 * @version    Ver 1.3.3_modularization
	 * @param  @return
	 * @return Bitmap
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-25下午5:22:39      
	*/
	public Bitmap getGifFlagBitmap(){
		if (mGifFlagBitmap==null){
			mGifFlagBitmap=BitmapFactory.decodeResource(IMSdkEntry.INSTANCE.context.getResources(),R.drawable.icon_gif);	
		}
		return mGifFlagBitmap;
	}
	/**
	 * @n<b>函数名称</b>     :getStartBitmap
	 * @brief 获取开始下载的图标
	 * @version    Ver 1.3.3_modularization
	 * @param  @return
	 * @return Bitmap
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-25下午5:23:08      
	*/
	public Bitmap getStartBitmap(){
		if (mStartBitmap==null){
			mStartBitmap=BitmapFactory.decodeResource(IMSdkEntry.INSTANCE.context.getResources(),R.drawable.start);	
		}
		return mStartBitmap;
	}
	public void put(Context context,String key,Bitmap bitmap){
		if (mBitmapMaps.containsKey(context)){
			ConcurrentHashMap<String, Bitmap> map=mBitmapMaps.get(context);
			map.put(key, bitmap);
		}else{
			ConcurrentHashMap<String, Bitmap> map=new ConcurrentHashMap<String, Bitmap>();
			map.put(key, bitmap);
			mBitmapMaps.put(context, map);
		}
	}
	
	/**
	 * @n<b>函数名称</b>     :get
	 * @brief 从缓存里获取一张图片
	 * @version    Ver 1.3.3_modularization
	 * @param  @param key
	 * @param  @return
	 * @return Bitmap
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-23下午6:55:04      
	*/
	public Bitmap get(Context context,String key){
		if (TextUtils.isEmpty(key) || context==null){
			return null;
		}
		ConcurrentHashMap<String, Bitmap> bitmaps=mBitmapMaps.get(context);
		if (bitmaps==null){
			bitmaps=new ConcurrentHashMap<String, Bitmap>();
			mBitmapMaps.put(context, bitmaps);
		}
		if (bitmaps.containsKey(key)){
			//缓存里有该图片，直接取出
			return bitmaps.get(key);
		}
		try {
			//缓存里没有，根据路径生成
			Bitmap bmp=getThumbBitmap(key);
			if (bmp!=null){
				bitmaps.put(key, bmp);
			}
			return bmp;
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 根据路径获取缩略图
	 * <br>Created 2014-9-20 下午4:13:04
	 * @param key 路径
	 * @return 缩略图
	 * @author  cb
	*/
	private Bitmap getThumbBitmap(String key){
		Options mOptions = new BitmapFactory.Options();
        mOptions.inJustDecodeBounds = true;
        mOptions.inSampleSize = 1;
        BitmapFactory.decodeFile(key, mOptions);
        int maxSize=DisplayUtil.dip2px(IMSdkEntry.INSTANCE.context,MAX_WIDTH);
        int nowSize=Math.max(mOptions.outHeight, mOptions.outWidth);
        while (nowSize > maxSize) {
            mOptions.inSampleSize=mOptions.inSampleSize*2;
            nowSize = nowSize >> 2;
        }
        mOptions.inJustDecodeBounds = false;
        Bitmap mThumbnail = BitmapFactory.decodeFile(key, mOptions);
        return mThumbnail;
	}
	
	/**
	 * @n<b>函数名称</b>     :clear
	 * @brief 清空所有的缓存图片
	 * @version    Ver 1.3.3_modularization
	 * @param  
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-23下午6:56:06      
	*/
	public void clear(Context context){
		if (mBitmapMaps.isEmpty() || context==null){
			return;
		}
		Iterator<Entry<Context,ConcurrentHashMap<String, Bitmap>>> topIterator=mBitmapMaps.entrySet().iterator();
		while (topIterator.hasNext()){
			Entry<Context,ConcurrentHashMap<String, Bitmap>> topEntry=topIterator.next();
			Context curContext=topEntry.getKey();
			if (curContext.equals(context)){
				ConcurrentHashMap<String, Bitmap> bitmaps=topEntry.getValue();
				Iterator<Entry<String, Bitmap>> iterator=bitmaps.entrySet().iterator();
				while (iterator.hasNext()){
					Entry<String, Bitmap> entry=iterator.next();
					Bitmap bmp=entry.getValue();
					if (bmp!=null && !bmp.isRecycled()){
						CommonUtils.recycleBitmap(bmp);
						bmp=null;
					}
					iterator.remove();
				}
				topIterator.remove();
			}
		}
	}
}
